package net.kazed.nextaction.context.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import android.content.SharedPreferences;
import android.database.Cursor;

public class DueTasksActivity extends AbstractDatedTasksActivity {


    @Override
    protected int getContentViewId() {
        return R.layout.due_tasks;
    }
    
    /**
     * @return Number of days of display period from preference.
     */
    protected int getNumberOfDaysPreference() {
        SharedPreferences preferences = getPreferences(MODE_WORLD_READABLE);
        int numberOfDays = preferences.getInt("dueTasksPeriod", TaskDatePeriod.MONTH.getNumberOfDays());
        return numberOfDays;
    }

    /**
     * @param numberOfDays Number of days of display period to save in preference.
     */
    protected void saveNumberOfDaysPreference(int numberOfDays) {
        SharedPreferences preferences = getPreferences(MODE_WORLD_WRITEABLE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("dueTasksPeriod", numberOfDays);
        editor.commit();
    }

    /**
     * Create cursor for list view.
     * @param numberOfDays Number of days for selection period of started tasks.
     */
    protected Cursor createListCursor(ApplicationResource applicationResource, int numberOfDays) {
        Cursor cursor = null;
        Calendar calendar = GregorianCalendar.getInstance();
          calendar.roll(Calendar.DAY_OF_YEAR, numberOfDays);
          long endTime = calendar.getTimeInMillis();
          cursor = managedQuery(Task.CONTENT_URI, Task.FULL_PROJECTION.getFieldNames(), Task.Column.COMPLETE + " is null and "
                + Task.Column.DUE_DATE + " < ?",
                new String[] {Long.toString(endTime)},
                Task.Column.DUE_DATE + " ASC");
          setListAdapter(new TaskListAdapter(this, cursor, applicationResource));
          return cursor;
    }
        
}
